<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>13使用Javascript传递页面参数</title>

    <script src="../lib/js/jquery-3.2.1.min.js"></script>
    <script>
        $(function () {
            let params = getPageParams(window.location.href);

            if (!params) {
                let span = "<span>url中未携带参数</span>";
                $("#paramContainer").append(span);
            } else {
                for (let key in params) {
                    if (params.hasOwnProperty(key)) {
                        let value = params[key];
                        let span = `<span style="display: block;">${key} --------> ${value}</span>`;
                        $("#paramContainer").append(span);
                    }
                }
            }
        });

        function getPageParams(url) {
            let params = null;
            let index = url.indexOf("?");
            if (index > 0) {
                let path = url.substring(index + 1);
                if (path) {
                    let paramArr = path.split("&");
                    if (paramArr && paramArr.length) {
                        params = {};
                        paramArr.forEach((item) => {
                            if (item.indexOf("=") > 0) {
                                let nameValue = item.split("=");
                                if (nameValue.length === 1) {
                                    params[nameValue[0]] = "";
                                } else {
//                                console.log("key:" + nameValue[0] + ",value:" + nameValue[1]);
                                    params[nameValue[0]] = nameValue[1];
                                }
                            }
                        });
                    }
                }
            }
            return params;
        }
    </script>
</head>
<body>
<div id="paramContainer">

</div>
</body>
</html>